//
//  FilterHumanPlaysMisere.m
//  PrefCore
//
//  Created by Sergey on 6/5/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import "PSFilter.h"

// We should get here only if human player has declared misere in first two rounds only
PSHandHash PSFilterHumanPlaysMisere(
    PSEvaluator* evaluator
,   PSHandHash candidates
,   PSPlayerId player
,   PSTrickHash trick
,   uint32_t round
,   uint8_t handIndex
,   PSHandHash* hands
) {
    if (handIndex == 0) {
        /*
        PSPlayerId p1 = PSGameNextPlayerId(player);
        PSPlayerId p2 = PSGameNextPlayerId(p1);
        PSHandHash filtered1 = candidates;
        
        // filter out all candidates with suit that declarer does not have
        if (round < 2) {
            for (int i = 0 ; i != PS_CARD_SUIT_RISING_LEN ; i++) {
                PSCardSuit s = PSCardSuitRising[i];
                if (PSHandKeepSuit(hands[0], s) == 0) {
                    filtered1 = PSHandRemoveSuit(filtered1, s);
                }
            }
        }
        PSHandHash filtered2 = 0;
        
        // first hand player shouldn't move from card with suit that is no longer in game
        for (PSCardHash hash = [evaluator startWithCandidates:filtered1]
            ;   hash != 0
            ;   hash = [evaluator nextWithCandidates:filtered1 andCurrent:hash]) {
            PSCard *c = [PSCard cardWithHash:hash];
            if (PSHandKeepSuit(hands[p1], c.suit) != 0 || PSHandKeepSuit(hands[p2], c.suit) != 0) {
                filtered2 = PSHandAddCard(filtered2, c);
            }
        }
        candidates = filtered2 == 0 ? [evaluator startWithCandidates:candidates] : filtered2;
        */
    } else if (handIndex == 1) {
        if (round < 2) {
            if (PSTrickCardOwner(trick, 0) == 0) {
                PSCard *c = PSTrickCard(trick, 0);
                PSCardRankSet set1 = PSHandGetSuit(hands[player], c.suit);
                PSCardRank lower1 = PSCardSetNextLowerRank(set1, c.rank);
                if (lower1 == kRankNull && set1 != 0) {
                    return candidates;
                }
                PSPlayerId p2 = PSGameNextPlayerId(player);
                PSCardRankSet set2 = PSHandGetSuit(hands[p2], c.suit);
                PSCardRank lower2 = PSCardSetNextLowerRank(set2, c.rank);
                if (lower2 == kRankNull && set2 != 0) {
                    return candidates;
                }
                return lower1 == kRankNull ? candidates : PSHandWithOneCard([PSCard cardWithSuit:c.suit andRank:lower1]);
            }
        }
    } else {
        if (round < 2) {
            if (PSTrickCardOwner(trick, 0) == 0) {
                PSCard *c = PSTrickCard(trick, 0);
                if (c == PSTrickHighestCard(trick, kSuitNone)) {
                    PSCardRankSet set = PSHandGetSuit(hands[player], c.suit);
                    PSCardRank lower = PSCardSetNextLowerRank(set, c.rank);
                    if (lower != kRankNull) {
                        return PSHandWithOneCard([PSCard cardWithSuit:c.suit andRank:lower]);
                    }
                }
            }
        }
    }
    return candidates;
}
